/**
 * Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
 */
package com.xinpan.exercise;

public class LongestCommonPrefix {
    public String longestCommonPrefix(String[] strs) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if(strs.length == 0)
            return "";
            
        String com = "";
        for(int i = 0; i < strs[0].length(); i++)
        {
            for(int j = 0; j < strs.length; j++)
            {
                if(strs[j].length()-1 < i || strs[j].charAt(i) != strs[0].charAt(i))
                    return com;
            }
            com += strs[0].charAt(i);
        }
        return com;
    }
}
